#include "PclFastGreedyProjectionTriangulationModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.surfaces_fast_greedy_projection_triangulation.json";
}

PclFastGreedyProjectionTriangulationModel::PclFastGreedyProjectionTriangulationModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/greedy_projection.html");
}

PclFastGreedyProjectionTriangulationModel& PclFastGreedyProjectionTriangulationModel::get()
{
    static PclFastGreedyProjectionTriangulationModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void PclFastGreedyProjectionTriangulationModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);
    this->pcdFile_ = json["pcdFile"].toString(pcdFile_);
    this->scale_ = json["scale"].toDouble(scale_);
    this->kNearestNeighbors_ = json["kNearestNeighbors"].toInt(kNearestNeighbors_);
    this->searchRadius_ = json["searchRadius"].toDouble(searchRadius_);
    this->mu_ = json["mu"].toDouble(mu_);
    this->maximumNearestNeighbors_ = json["maximumNearestNeighbors"].toInt(maximumNearestNeighbors_);
    this->maximumSurfaceAngle_ = json["maximumSurfaceAngle"].toDouble(maximumSurfaceAngle_);
    this->minimumAngle_ = json["minimumAngle"].toDouble(minimumAngle_);
    this->maximumAngle_ = json["maximumAngle"].toDouble(maximumAngle_);
    this->normalConsistency_ = json["normalConsistency"].toBool(normalConsistency_);
}

QJsonObject PclFastGreedyProjectionTriangulationModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();
    json.insert("pcdFile", QJsonValue(this->pcdFile_));
    json.insert("scale", QJsonValue(this->scale_));
    json.insert("kNearestNeighbors", QJsonValue(this->kNearestNeighbors_));
    json.insert("searchRadius", QJsonValue(this->searchRadius_));
    json.insert("mu", QJsonValue(this->mu_));
    json.insert("maximumNearestNeighbors", QJsonValue(this->maximumNearestNeighbors_));
    json.insert("maximumSurfaceAngle", QJsonValue(this->maximumSurfaceAngle_));
    json.insert("minimumAngle", QJsonValue(this->minimumAngle_));
    json.insert("maximumAngle", QJsonValue(this->maximumAngle_));
    json.insert("normalConsistency", QJsonValue(this->normalConsistency_));

    return json;
}
